package com.huping.controller;


import com.alibaba.fastjson.JSONObject;
import com.huping.pojo.PageBean;
import com.huping.pojo.Result;
import com.huping.service.GradeService;
import com.huping.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
public class GradeController {
    @Autowired
    private GradeService gradeService;

    @PostMapping("/grade/get-students-homework")
    public Result getStudentsHomework(@RequestHeader String token , @RequestBody JSONObject param){
        Claims claims= JwtUtils.parseJWT(token);
        PageBean pageBean;
        String userid=(String)claims.get("userid");
        String kind=(String)claims.get("kind");
        if(kind.equals("1")){
            return    Result.error("学生不能使用此接口");
        }
        System.out.println(userid);
       return gradeService.getAllStudentsHomeworkByHomeworkId(userid,param);

    }

    @PostMapping("/grade/teacher-grade")
    public Result teacherGiveStudentGrade(@RequestHeader String token , @RequestBody JSONObject param){
        Claims claims= JwtUtils.parseJWT(token);
        String kind=(String)claims.get("kind");
        if(kind.equals("1")){
            return    Result.error("学生不能使用此接口");
        }

        int score;
        try{
            score=  Integer.parseInt(param.getString("score"));
        } catch (NumberFormatException e){
            e.printStackTrace();
            return Result.error("请输入正确的分数");
        }

      return   gradeService.teacherGiveStudentGrade(Integer.parseInt(param.getString("homeworkId")), param.getString("studentId"),score,param.getString("comment") );



    }

    @PostMapping("/grade/get-huping-homework")
    public Result studentGetHupingHomework(@RequestHeader String token , @RequestBody JSONObject param){
        Claims claims=JwtUtils.parseJWT(token);
        String kind=(String)claims.get("kind");
        if(kind.equals("2")){
            return    Result.error("教师不能获得互评作业");
        }
        String userid=(String)claims.get("userid");
     return    gradeService.studentGetHupingHomework(userid,param);

    }


  @PostMapping("/grade/get-student-homework-by-sequence")
    public Result getStudentHomeworkBySequence(@RequestHeader String token , @RequestBody JSONObject param){

      Claims claims=JwtUtils.parseJWT(token);
      String userid=(String)claims.get("userid");
       return gradeService.getHomeworkBySequence(userid,param);

  }

@PostMapping("/grade/huping-grade")
    public Result hupingGrade(@RequestHeader String token , @RequestBody JSONObject param){
    Claims claims=JwtUtils.parseJWT(token);
    String userid=(String)claims.get("userid");

        return gradeService.updateHupingScoreInScoreRecord(userid,param);
}

@PostMapping("/grade/select-historical-reading")
    public  Result selectHistoricalReading(@RequestHeader String token , @RequestBody JSONObject param){

    Claims claims=JwtUtils.parseJWT(token);
    String userid=(String)claims.get("userid");
    PageBean pageBean;
    pageBean= gradeService.selectHistoricalReading(userid,param);
    return Result.success(pageBean);


}


@PostMapping("/grade/grade-statistic")
    public  Result getGradeStatistic( @RequestBody JSONObject param){

        return  gradeService.getGradeStatistic(param);
}

@PostMapping("/grade/get-excellent-homework")
public  Result getExcellentHomework( @RequestBody JSONObject param){

        return gradeService.getExcellentHomework(param);
}


@PostMapping("/grade/set-excellent")
    public  Result setExcellent(@RequestHeader String token,@RequestBody JSONObject param){
          Claims claims=JwtUtils.parseJWT(token);
          String kind=(String)claims.get("kind");
          if (kind.equals("1")){
              return  Result.error("学生不能设置优秀作业");
          }
        return  gradeService.setExcellentHomework(param);
}


@PostMapping("/grade/get-not-submit")
    public Result getNotSubmit(@RequestBody JSONObject param){


        return gradeService.getNotSubmit(param);

}




    @PostMapping("/grade/set-method")
    public  Result setMethod(@RequestHeader String token,@RequestBody JSONObject param){
        Claims claims=JwtUtils.parseJWT(token);
        String kind=(String)claims.get("kind");
        if(kind.equals("1")){
            return    Result.error("学生不能设置互评算法类型");
        }

        return gradeService.setMethod(param);

    }

    @PostMapping("/grade/get-huping-total-number")
    public Result getTotalNumber(@RequestHeader String token,@RequestBody JSONObject param){
        Claims claims=JwtUtils.parseJWT(token);
        String userid=(String)claims.get("userid");
        return gradeService.getTotalNumber(userid,param);

    }


}
